package com.lyn.utils;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import java.util.HashMap;
import java.util.Locale;

/**
 * TTS语音工具类
 * @author longyn 2022/12/29
 * @version 1.0.0
 */
public class TTSUtil {
    private static class TTS_ToolClassInstance {
        private static final TTSUtil instance = new TTSUtil();
    }

    public static TTSUtil getInstance() {
        return TTS_ToolClassInstance.instance;
    }

    public TextToSpeech textToSpeech;
    private final HashMap<String, String> ttsOptions = new HashMap<>();


    public void init(Context context) {
        try {
            textToSpeech = new TextToSpeech(context, status -> {
                if (status == TextToSpeech.SUCCESS) {
                    textToSpeech.setSpeechRate(1f);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //设置语速
    public void setSpeechRate(float speed) {
        if (textToSpeech != null) {
            textToSpeech.setSpeechRate(speed);
        }
    }

    public void setLanguage(final Locale loc) {
        try {
            textToSpeech.setLanguage(loc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置播报监听
     */
    public void setOnUtteranceProgressListener(UtteranceProgressListener onUtteranceProgressListener) {
        try {
            textToSpeech.setOnUtteranceProgressListener(onUtteranceProgressListener);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void close() {
        try {
            if (textToSpeech != null) {
                textToSpeech.stop();
                textToSpeech.shutdown();
                textToSpeech = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean pronounce(String s) {
        return pronounce(s, s, TextToSpeech.QUEUE_FLUSH);
    }

    public boolean pronounce(String s, int queueMode) {
        return pronounce(s, s, queueMode);
    }

    public synchronized boolean pronounce(String s, String tag, int queueMode) {
        try {
            if (textToSpeech == null) {
                return false;
            }
            ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, tag);
            textToSpeech.speak(s, queueMode, ttsOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean refresh() {
        if (textToSpeech == null) {
            return false;
        }
        ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "refresh");
        textToSpeech.speak("", TextToSpeech.QUEUE_FLUSH, ttsOptions);
        return true;
    }


    public boolean isSpeaking() {
        if (textToSpeech == null) return false;
        return textToSpeech.isSpeaking();
    }

    public void stop() {
        try {
            if (textToSpeech != null) {
                textToSpeech.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
